﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility.Config;
using System.Text;
using System.Data;
using Utility.Model;
using Utility;

namespace MsgCenter.Web
{
    public partial class Master : System.Web.UI.MasterPage
    {
        /// <summary>
        /// IP
        /// </summary>
        public string IP
        {
            get
            {
                string _ip = string.Empty;
                try
                {
                    if (Context.Request.ServerVariables["HTTP_VIA"] != null) // using proxy
                    {
                        _ip = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP.
                    }
                    else// not using proxy or can't get the Client IP
                    {
                        _ip = Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
                    }
                }
                catch
                { }
                return _ip;
            }
        }
        /// <summary>
        /// 用户登录信息
        /// </summary>
        protected UserSession UserSessionAttr
        {
            get
            {
                UserSession modelUserSession = BaseUI.UserSessionService.GetUserInfo(Page.User.Identity.Name, IP);
                if (modelUserSession == null)
                {
                    string script = " alert('提示：您未登陆或登陆时间已过期,请进行登陆');   top.location.href='" + ConfigHelper.WebHomeUrl + "LoginOut.aspx'";
                    Response.Write("<script language=\"javascript\" type=\"text/javascript\" defer>\n" + script + "\n</script>\n");
                    Response.End();
                }
                return modelUserSession;
            }
        }
        /// <summary>
        /// ModuleID页面参数值主键ID
        /// </summary>
        protected string strRequestID = ParamsOfRequest.Request("ModuleID").ToLower();
        /// <summary>
        /// 模块名称
        /// </summary>
        protected string strRequestName = "控制台";
        /// <summary>
        /// 登陆人姓名
        /// </summary>
        protected string strName { get { return UserSessionAttr.Name; } }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 得到左边导航树
        /// </summary>
        /// <returns></returns>
        protected string GetLeftMune()
        {
            string strLinkAddress = string.Empty;//连接地址
            string strModuleName = string.Empty;//名称
            string strZJID = string.Empty;//主键ID
            string strModuleID = string.Empty;//ModuleID
            StringBuilder strBuild = new StringBuilder();
            List<Sys_Module> dicModule = this.UserSessionAttr.UserModule;
            int len = 0;
            using (DataTable dt = dicModule.ToDataTable())
            {
                DataRow[] drsRow = dt.Select("FatherID='000001'", "Sort");
                foreach (DataRow drRow in drsRow)
                {
                    strModuleID = drRow["ModuleID"].ToString();
                    strModuleName = drRow["ModuleName"].ToString();
                    strZJID = drRow["ID"].ToString();
                    strLinkAddress = drRow["LinkAddress"].ToString();
                    DataRow[] drsChildRow = dt.Select("FatherID='" + strModuleID + "'", "Sort");
                    if (drsChildRow.Length > 0)//是否有第二级菜单
                    {
                        AddTowNodes(strModuleName, strBuild, drsChildRow, len);
                    }
                    else
                    {
                        if (strZJID == strRequestID)
                        {
                            strRequestName = strModuleName;
                            strBuild.Append("<li class='active'>").AppendLine();
                            strBuild.Append(GetUrlAddress(strLinkAddress, strModuleName, strZJID)).AppendLine();
                            strBuild.Append("</li>").AppendLine();
                        }
                        else
                        {
                            strBuild.Append("<li>").AppendLine();
                            strBuild.Append(GetUrlAddress(strLinkAddress, strModuleName, strZJID)).AppendLine();
                            strBuild.Append("</li>").AppendLine();
                        }
                    }
                    len++;
                }
            }
            return strBuild.ToString();
        }
        /// <summary>
        /// 创建第二级菜单
        /// </summary>
        /// <param name="strModuleName"></param>
        /// <param name="strBuild"></param>
        /// <param name="drsChildRow"></param>
        private void AddTowNodes(string strModuleName, StringBuilder strBuild, DataRow[] drsChildRow, int len)
        {
            string strMenu = string.Empty;
            bool result = GetTwoNode(drsChildRow, out strMenu);//如果主键ID和页面参数ModelID相等返回true
            if (result)
            {
                strBuild.Append("<li class='active open'>").AppendLine();
            }
            else
            {
                if (strRequestID == string.Empty)
                {
                    if (len == 0)
                    {
                        strBuild.Append("<li>").AppendLine();
                    }
                    else
                    {
                        strBuild.Append("<li>").AppendLine();
                    }
                }
                else
                {
                    strBuild.Append("<li>").AppendLine();
                }
            }
            strBuild.Append("<a href='#' class='dropdown-toggle'><i class='icon-dashboard'></i><span class='menu-text'>" + strModuleName + "</span> <b class='arrow icon-angle-down'></b></a>").AppendLine();
            strBuild.Append("<ul class='submenu'>").AppendLine();
            strBuild.Append(strMenu);
            strBuild.Append("</ul>").AppendLine();
            strBuild.Append("</li>").AppendLine();
        }
        /// <summary>
        /// 创建第二级菜单
        /// </summary>
        /// <param name="drsRow"></param>
        /// <param name="strMune"></param>
        /// <returns></returns>
        private bool GetTwoNode(DataRow[] drsRow, out string strMune)
        {
            bool reulst = false;
            StringBuilder strBuild = new StringBuilder();
            string strLinkAddress = string.Empty;
            string strModuleName = string.Empty;
            string strZJID = string.Empty;
            foreach (DataRow drRow in drsRow)
            {
                strLinkAddress = drRow["LinkAddress"].ToString();
                strModuleName = drRow["ModuleName"].ToString();
                strZJID = drRow["ID"].ToString();
                if (strZJID == strRequestID)//如果主键ID和页面参数ModelID相等返回true
                {
                    strRequestName = strModuleName;
                    strBuild.Append("<li class='active'>").AppendLine();
                    strBuild.Append(GetUrlAddress(strLinkAddress, strModuleName, strZJID)).AppendLine();
                    strBuild.Append("</li>").AppendLine();
                    reulst = true;
                }
                else
                {
                    strBuild.Append("<li>").AppendLine();
                    strBuild.Append(GetUrlAddress(strLinkAddress, strModuleName, strZJID)).AppendLine();
                    strBuild.Append("</li>").AppendLine();
                }
            }
            strMune = strBuild.ToString();
            return reulst;
        }

        private string GetUrlAddress(string strLinkAddress, string strModuleName, string strModelID)
        {
            if (strLinkAddress.IndexOf("?") > -1)
            {
                strLinkAddress = strLinkAddress + "&ModuleID=" + strModelID;
            }
            else
            {
                strLinkAddress = strLinkAddress + "?ModuleID=" + strModelID;
            }
            return string.Format("<a href='{0}'><i class='icon-dashboard'></i><span class='menu-text'>{1}</span></a>", string.Concat(ConfigHelper.WebHomeUrl, strLinkAddress), strModuleName);
        }
    }
}